import json
from typing import Literal, Union, Dict
from webapp import redis_store


def get(
        key: str,
        content_type: Literal['str', 'json', 'int'] = 'json',
):
    result = redis_store.get(key)
    if result is not None:
        if content_type == 'json':
            result = json.loads(result)
        elif content_type == 'int':
            result = int(result)
    return result


def delete(key: str):
    return redis_store.delete(key)


def hset(
        key: str,
        field: str,
        value: Union[str, int, dict, list],
):
    if not isinstance(value, (str, int)):
        value = json.dumps(value)
    return redis_store.hset(key, field, value)


def hget(key: str, field: str, content_type: Literal['str', 'json', 'int'] = 'json'):
    result = redis_store.hget(key, field)
    if result and content_type == 'json':
        result = json.loads(result)
    elif result and content_type == 'int':
        result = int(result)
    return result


def hgetall(key: str, content_type: Literal['str', 'json', 'int'] = 'json'):
    result = redis_store.hgetall(key)
    if content_type in ('json', 'int'):
        for key, value in result.items():
            if content_type == 'json':
                result[key] = json.loads(value)
            elif content_type == 'int':
                result[key] = int(value)
    return result


def hdel(key: str, field: str):
    return redis_store.hdel(key, field)


def hlen(key: str):
    return redis_store.hlen(key)


def hmset(
        key: str,
        value: Dict[str, str]
):
    return redis_store.hmset(key, value)
